package com.yuanqunsoft.alimigration.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.RuleBean;
import com.yuanqunsoft.alimigration.bean.RuleSetBean;
import com.yuanqunsoft.alimigration.dao.IRuleSetDao;
import com.yuanqunsoft.alimigration.model.AllUser;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleSet;
import com.yuanqunsoft.alimigration.service.IRuleSetService;

public class RuleSetServiceImpl implements IRuleSetService {

    private IRuleSetDao ruleSetDao;

    @Override
    public List<RuleBean> getRuleByRuleSetId(int id) {
        return ruleTransToBean(ruleSetDao.getRuleByRuleSetId(id));
    }

    @Override
    public boolean addRuleSet(RuleSetBean rs, List<Integer> ruleIds) {
        RuleSet r = getRuleSetFromBean(rs);
        return ruleSetDao.save(r, ruleIds);
    }

    @Override
    public boolean deleteRuleSet(RuleSetBean rs) {
        RuleSet r = this.ruleSetDao.getRuleSetById(rs.getRusId());
        return ruleSetDao.delete(r);
    }

    @Override
    public boolean updateRuleSet(RuleSetBean rs) {
        // TODO Auto-generated method stub
        RuleSet r = this.ruleSetDao.getRuleSetById(rs.getRusId());
        return ruleSetDao.update(r);
    }

    @Override
    public List<RuleSetBean> getAllRuleSets() {
        // TODO Auto-generated method stub
        return transToBean(ruleSetDao.getAllRuleSet());
    }

    @Override
    public List<RuleSetBean> getAllRuleSets(int currentPage, int pageSize) {
        // TODO Auto-generated method stub
        return transToBean(ruleSetDao.queryPage(currentPage, pageSize));
    }

    @Override
    public RuleSetBean getRuleSetById(int id) {
        // TODO Auto-generated method stub
        return transToBean(ruleSetDao.getRuleSetById(id));
    }

    @Override
    public int getAllPage() {
        // TODO Auto-generated method stub
        return ruleSetDao.getAllPage();
    }

    @Override
    public void selectRules(RuleSetBean rs, List<Integer> ruleIds) {
        // TODO Auto-generated method stub
        RuleSet r = this.ruleSetDao.getRuleSetById(rs.getRusId());
        ruleSetDao.selectRules(r, ruleIds);
    }

    public IRuleSetDao getRuleSetDao() {
        return ruleSetDao;
    }

    public void setRuleSetDao(IRuleSetDao ruleSetDao) {
        this.ruleSetDao = ruleSetDao;
    }

    @Override
    public boolean isRuleSetNameExisted(RuleSetBean ruleSet) {
        // TODO Auto-generated method stub
        List<RuleSet> rs = this.ruleSetDao.getAllRuleSet();
        Iterator<RuleSet> i = rs.iterator();
        while(i.hasNext()){
            if(i.next().getRusName().equals(ruleSet.getRusName())){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<RuleSetBean> findByRuleId(int id) {
        return transToBean(ruleSetDao.findByRuleId(id));
    }
  
    public RuleSetBean transToBean(RuleSet rs){
        
        RuleSetBean rsb = new RuleSetBean();
        
        rsb.setRusCreateTime(rs.getRusCreateTime());
        rsb.setRusDescription(rs.getRusDescription());
        rsb.setRusId(rs.getRusId());
        rsb.setRusName(rs.getRusName());
        rsb.setRusPublic(rs.getRusPublic());
        rsb.setRusReleaseStatus(rs.getRusReleaseStatus());
        rsb.setRusVersion(rs.getRusVersion());
        rsb.setUserId(rs.getAllUser().getUserId());
        rsb.setUserName(rs.getAllUser().getUserName());
        return rsb;
    }
    
    public List<RuleSetBean> transToBean(List<RuleSet> ruleSets){
        
        Iterator<RuleSet> rss = ruleSets.iterator();
        List<RuleSetBean> rsbs = new ArrayList<RuleSetBean>();
        RuleSetBean b;
        RuleSet rs;
        while(rss.hasNext()){
            rs = rss.next();
            b = new RuleSetBean();
            b.setRusCreateTime(rs.getRusCreateTime());
            b.setRusDescription(rs.getRusDescription());
            b.setRusId(rs.getRusId());
            b.setRusName(rs.getRusName());
            b.setRusPublic(rs.getRusPublic());
            b.setRusReleaseStatus(rs.getRusReleaseStatus());
            b.setRusVersion(rs.getRusVersion());
            b.setUserId(rs.getAllUser().getUserId());
            b.setUserName(rs.getAllUser().getUserName());
            rsbs.add(b);
        }
        return rsbs;
    }
    
    public List<RuleBean> ruleTransToBean(List<Rule> rules){
        Iterator<Rule> i = rules.iterator();
        List<RuleBean> rbs = new ArrayList<RuleBean>();
        Rule rule;
        RuleBean rb;
        while(i.hasNext()){
            rule = i.next();
            rb = new RuleBean();
            rb.setRulAllFurtherDayEach(rule.getRulAllFurtherDayEach());
            rb.setRulCategory(rule.getRulCategory());
            rb.setRulDescription(rule.getRulDescription());
            rb.setRulEnabled(rule.getRulEnabled());
            rb.setRulFirstAlerts(rule.getRulFirstAlerts());
            rb.setRulFirstDaysEach(rule.getRulFirstDaysEach());
            rb.setRulNextAlerts(rule.getRulNextAlerts());
            rb.setRulNextDaysEach(rule.getRulNextDaysEach());
            rb.setRulId(rule.getRulId());
            rb.setRulName(rule.getRulName());
            rb.setRulSeverity(rule.getRulSeverity());
            rbs.add(rb);
        }
       return rbs;
    }
    
    public RuleSet getRuleSetFromBean(RuleSetBean rs){
        RuleSet rsb = new RuleSet();
        rsb.setRusCreateTime(rs.getRusCreateTime());
        rsb.setRusDescription(rs.getRusDescription());
        rsb.setRusId(rs.getRusId());
        rsb.setRusName(rs.getRusName());
        rsb.setRusPublic(rs.getRusPublic());
        rsb.setRusReleaseStatus(rs.getRusReleaseStatus());
        rsb.setRusVersion(rs.getRusVersion());
        AllUser user = new AllUser();
        user = this.ruleSetDao.getUserById(rs.getUserId());
        rsb.setAllUser(user);
        return rsb;
    }

}
